package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.Service.OrderService;

import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderInfo;
import com.itheima.pojo.OrderSetting;
import com.itheima.uitils.DateUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;

import java.util.List;
import java.util.Map;

/*- 理解业务需求

1、
2、
3、
4、检查当前用户是否为会员，如果是会员则直接完成预约，如果不是会员则自动完成注册并进行预约
5、预约成功，更新当日的已预约人数*/
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private SetmealDao setmealDao;


    public Result order(OrderInfo orderInfo) {

        //检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(orderInfo.getOrderDate());
        if(orderSetting == null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
        if(orderSetting.getReservations() >= orderSetting.getNumber()){
            return new Result(false,MessageConstant.ORDER_FULL);
        }

        //检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约则无法完成再次预约
        //查用户表，查预约表
        Member member = memberDao.findByTelephone(orderInfo.getTelephone());
        //根据手机号查询会员表若有则已为会员
        if(member != null){
           //已有会员账号则查询预约表
            Integer memberId = member.getId();
            Integer setmealId = orderInfo.getSetmealId();
            Date orderDate = orderInfo.getOrderDate();
            Order order = new Order(memberId,orderDate,setmealId);
            List<Order> list = orderDao.findByCondition(order);
            if(list != null && list.size() > 0){//同天同会员同套餐，重复了
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }else {
            //根据手机号查询会员表若无则为第一次使用,默认注册
            member = new Member();
            member.setName(orderInfo.getName());

            member.setPhoneNumber(orderInfo.getTelephone());
            member.setIdCard(orderInfo.getIdCard());
            member.setSex(orderInfo.getSex());
            member.setRegTime(new Date());
            memberDao.add(member);//返回主键ID

        }
        //添加预约
        Order order = new Order();
        order.setOrderDate(orderInfo.getOrderDate());
        order.setSetmealId(orderInfo.getSetmealId());
        order.setMemberId(member.getId());
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        orderDao.add(order);


        //预约成功后添加已预约人数
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingDao.editByOrderDate(orderSetting);





        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    //根据id查询预约详细信息（包括会员姓名、套餐名称、预约基本信息）
    public Map findById(Integer id) {
        Map map = orderDao.findDetailById(id);
        if(map != null){
            //处理日期格式
            Date orderDate = (Date) map.get("orderDate");
            try {
                map.put("orderDate",DateUtils.parseDate2String(orderDate));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        /*String orderDate = (String) map.get("orderDate");
        try {
            //对时间格式修改yyyy-MM-dd
            Date date = DateUtils.parseString2Date(orderDate);
            map.put("orderDate",date);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        return map;

    }



}
